﻿Public Class QuanLyNoForm
    Dim dt As DataTable
    Dim dt_nganhang As DataTable
    Dim dt_chiphi As DataTable
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Me.Dispose()
    End Sub

    Private Sub NganHangForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dt_chiphi = ChiPhiDAO.GetAll()
        cbMaChiPhi.DataSource = dt_chiphi
        cbMaChiPhi.DisplayMember = "Ten"
        cbMaChiPhi.ValueMember = "MaChiPhi"

        dt_nganhang = NganHangDAO.GetAll()
        cbMaNganHang.DataSource = dt_nganhang
        cbMaNganHang.DisplayMember = "Ten"
        cbMaNganHang.ValueMember = "MaNganHang"

        dt = QuanLyNoDAO.GetAll(MainForm.current_user.MaUser)
        dg.DataSource = dt
        dg.Columns("SoTien").HeaderText = "Số Tiền"
        dg.Columns("ChiTiet").HeaderText = "Chi Tiết"
        dg.Columns("LaiSuat").HeaderText = "Lãi Suất"
        dg.Columns("NgayMuon").HeaderText = "Ngày Mượn"
        dg.Columns("TinhTrang").HeaderText = "Đáo Hạn"
        dg.Columns("KyHan").HeaderText = "Kỳ Hạn"
        dg.Columns("DaThanhToan").HeaderText = "Đã Thanh Toán"
        dg.Columns("MaNo").Visible = False
        dg.Columns("MaChiPhi").Visible = False
        dg.Columns("MaNganHang").Visible = False
        dg.Columns("MaUser").Visible = False
        dtsNgayMuon.Format = DateTimePickerFormat.Custom
        dtsNgayMuon.CustomFormat = " "

        Dim btn As New DataGridViewButtonColumn()
        btn.HeaderText = ""
        btn.Text = "Thanh Toán"
        btn.Name = "btn"
        btn.UseColumnTextForButtonValue = True        
        dg.Columns.Add(btn)
    End Sub

    Private Sub dgNganHang_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellClick        
        If Not IsNothing(dg.CurrentRow) And dg.CurrentRow.Index >= 0 Then
            If dg.CurrentCell.Value.ToString() = "Thanh Toán" Then
                If MessageBox.Show("Bạn thật sự muốn trả nợ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    If QuanLyNoBus.TraNo(dg.CurrentRow.Cells("MaNo").Value) Then
                        MessageBox.Show("Yêu cầu thanh toán đã được thực hiện.")
                        dt = QuanLyNoDAO.GetAll(MainForm.current_user.MaUser)
                        dg.DataSource = dt
                    Else
                        MessageBox.Show("Nợ này đã được trả trước đó.")
                    End If
                End If
            Else
                txtSoTien.Text = dt.Rows(dg.CurrentRow.Index).Item("SoTien").ToString()
                txtLaiSuat.Text = dt.Rows(dg.CurrentRow.Index).Item("LaiSuat").ToString()
                txtChiTiet.Text = dt.Rows(dg.CurrentRow.Index).Item("ChiTiet").ToString()
                dtNgayMuon.Text = dt.Rows(dg.CurrentRow.Index).Item("NgayMuon").ToString()
                If Integer.Parse(dt.Rows(dg.CurrentRow.Index).Item("TinhTrang")) Then
                    cbDaoHan.Checked = True
                Else
                    cbDaoHan.Checked = False
                End If
            End If
        End If
    End Sub

    Private Sub btThem_Click(sender As System.Object, e As System.EventArgs) Handles btThem.Click
        If txtSoTien.Text.Trim().Length = 0 Or Not IsNumeric(txtSoTien.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào số tiền")
            Return
        End If
        If txtLaiSuat.Text.Trim().Length = 0 Or Not IsNumeric(txtLaiSuat.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào lãi suất")
            Return
        End If
        If txtChiTiet.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng nhập vào chi tiết")
            Return
        End If
        
        Dim dr As DataRow = dt.NewRow()
        dr("MaUser") = MainForm.current_user.MaUser        
        dr("SoTien") = Double.Parse(txtSoTien.Text)
        dr("ChiTiet") = txtChiTiet.Text        
        dr("NgayMuon") = dtNgayMuon.Text        
        dr("LaiSuat") = Double.Parse(txtLaiSuat.Text)
        dr("MaNganHang") = cbMaNganHang.SelectedValue
        dr("MaChiPhi") = cbMaChiPhi.SelectedValue
        dr("KyHan") = txtKyHan.Text
        dr("DaThanhToan") = 0
        If cbDaoHan.Checked Then
            dr("TinhTrang") = 1
        Else
            dr("TinhTrang") = 0
        End If
        dt.Rows.Add(dr)
        DB_Provider.Write(dt, "QuanLyNo")
    End Sub

    Private Sub btXoa_Click(sender As System.Object, e As System.EventArgs) Handles btXoa.Click
        If dt.Rows.Count > 0 Then
            dt.Rows(dg.CurrentRow.Index).Delete()
            DB_Provider.Write(dt, "QuanLyNo")
        End If
    End Sub

    Private Sub btSua_Click(sender As System.Object, e As System.EventArgs) Handles btSua.Click
        If txtSoTien.Text.Trim().Length = 0 Or Not IsNumeric(txtSoTien.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào số tiền")
            Return
        End If
        If txtLaiSuat.Text.Trim().Length = 0 Or Not IsNumeric(txtLaiSuat.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào lãi suất")
            Return
        End If
        If txtChiTiet.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng nhập vào chi tiết")
            Return
        End If
        Dim index As Integer = dg.CurrentRow.Index
        If (index > -1) Then
            dt.Rows(index).Item("SoTien") = Double.Parse(txtSoTien.Text)
            dt.Rows(index).Item("ChiTiet") = txtChiTiet.Text
            dt.Rows(index).Item("NgayMuon") = dtNgayMuon.Text
            dt.Rows(index).Item("LaiSuat") = txtLaiSuat.Text
            dt.Rows(index).Item("MaNganHang") = cbMaNganHang.SelectedValue
            dt.Rows(index).Item("MaChiPhi") = cbMaChiPhi.SelectedValue
            dt.Rows(index).Item("KyHan") = txtKyHan.Text
            If cbDaoHan.Checked Then
                dt.Rows(index).Item("TinhTrang") = 1
            Else
                dt.Rows(index).Item("TinhTrang") = 0
            End If
            DB_Provider.Write(dt, "QuanLyNo")
        End If
    End Sub

    Private Sub dg_CellFormatting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dg.CellFormatting
        For Each dr As DataGridViewRow In dg.Rows
            If dr.Cells("TinhTrang").Value Then
                dr.Cells("TinhTrang").Style.Format = "Có"
            Else
                dr.Cells("TinhTrang").Style.Format = "Không"
            End If
            If dr.Cells("DaThanhToan").Value.ToString() = "" Or dr.Cells("DaThanhToan").Value = 0 Then
                dr.Cells("DaThanhToan").Style.Format = "Chưa"
            Else
                dr.Cells("DaThanhToan").Style.Format = "Rồi"
            End If
        Next
    End Sub

    Private Sub btnTim_Click(sender As System.Object, e As System.EventArgs) Handles btnTim.Click
        Dim arr As New List(Of String)
        If txtsChiTiet.Text.Length > 0 Then
            arr.Add(String.Format("ChiTiet like '%{0}%'", txtsChiTiet.Text))
        End If
        If txtsSoTien.Text.Length > 0 Then
            arr.Add(String.Format("SoTien >= {0}", txtsSoTien.Text))
        End If
        If dtsNgayMuon.Text.Trim().Length > 0 Then
            arr.Add(String.Format("NgayMuon >= #{0}#", dtsNgayMuon.Text))
        End If
        Dim filter As String = String.Join(" and ", arr)
        dt.DefaultView.RowFilter = filter
    End Sub

    Private Sub dtsNgayMuon_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtsNgayMuon.ValueChanged        
        dtsNgayMuon.CustomFormat = "yyyy-MM-dd"
    End Sub

    Private Sub btnThemChiPhi_Click(sender As System.Object, e As System.EventArgs) Handles btnThemChiPhi.Click
        Dim f As New ChiPhiForm
        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            dt_chiphi = ChiPhiDAO.GetAll()
            cbMaChiPhi.DataSource = dt_chiphi
            cbMaChiPhi.DisplayMember = "Ten"
            cbMaChiPhi.ValueMember = "MaChiPhi"
        End If
    End Sub

    Private Sub btnThemNganHang_Click(sender As System.Object, e As System.EventArgs) Handles btnThemNganHang.Click
        Dim f As New NganHangForm
        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            dt_nganhang = NganHangDAO.GetAll()
            cbMaNganHang.DataSource = dt_nganhang
            cbMaNganHang.DisplayMember = "Ten"
            cbMaNganHang.ValueMember = "MaNganHang"
        End If
    End Sub
End Class